import top.codedance.iotp.common.util.HexCodec;
import top.codedance.iotp.iot.sdk.*;
import top.codedance.iotp.iot.sdk.entity.DistributedRegistyUris;
import top.codedance.iotp.iot.sdk.po.async.*;
import top.codedance.iotp.loadblance.entiy.RegistyUri;
import org.junit.Test;

public class DistributedWsTestSdk {
    private HexCodec hc = new HexCodec();

    /**
     * 系统命令异频执行与监听
     */
    @Test
    public void testSimpleClientForSystem() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "system", "u3", "u3", "d3");
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        distributedWsSimpleClient.send(new WsSystemPowershellParam(false));

//        distributedWsSimpleClient.waiting(5);
//        distributedWsSimpleClient.send(new WsSystemExecCommandParam("ping -t 127.0.0.1"));
//        distributedWsSimpleClient.waiting(10);

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);
                distributedWsSimpleClient.send(new WsSystemExecCommandParam("netstat -ano"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * MQTT异频执行与监听
     */
    @Test
    public void testSimpleClientForMqtt() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "mqtt", "u3", "u3", "d3", 0);
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        WsMqttSubscriptionParam wmsp = new WsMqttSubscriptionParam();
//        wmsp.add("/test/d1/voltage");
        wmsp.add("/test/d2/current");
        distributedWsSimpleClient.send(wmsp);

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);

                WsMqttPublishParam wsMqttPublishParam = new WsMqttPublishParam();
                WsMqttPublishItem item1 = new WsMqttPublishItem();
                item1.setTopic("/test/d2/current");
                WsMqttPublishMessage message1 = new WsMqttPublishMessage();
                message1.setQos(2);
                message1.setPayload(3);
                item1.setMessage(message1);
                wsMqttPublishParam.add(item1);
                distributedWsSimpleClient.send(wsMqttPublishParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * OPC异频执行与监听
     */
    @Test
    public void testSimpleClientForOPC() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "opc", "u3", "u3", "d3", 0);
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        WsOpcSubscriptionParam wsOpcSubscriptionParam = new WsOpcSubscriptionParam();
        wsOpcSubscriptionParam.add("模拟器示例.函数.Ramp1");
        wsOpcSubscriptionParam.add("模拟器示例.函数.Random1");
        distributedWsSimpleClient.send(wsOpcSubscriptionParam);

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);

                WsOpcPublishParam wsOpcPublishParam = new WsOpcPublishParam();
                WsOpcPublishItem item1 = new WsOpcPublishItem("通道 1.设备 1.a", 2, 88);
                wsOpcPublishParam.add(item1);
                WsOpcPublishItem item2 = new WsOpcPublishItem("通道 1.设备 1.b", 2, 0.56);
                wsOpcPublishParam.add(item2);
                distributedWsSimpleClient.send(wsOpcPublishParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * com异频执行与监听
     */
    @Test
    public void testSimpleClientForCOM() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10015", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "com", "u3", "u3", "d3", 0, true);
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }

            @Override
            public void error(String error) {
                System.out.println(error);
            }
        });

        try {
            while (true) {
                distributedWsSimpleClient.waiting(1);
                //直接传字符串
//                WsComExecCommandParam wsComExecCommandParam = new WsComExecCommandParam("ver");
                //传字节数组
                byte[] bytes = hc.toBytes("55AA010000");
                WsComExecCommandParam wsComExecCommandParam = new WsComExecCommandParam(bytes);
                distributedWsSimpleClient.send(wsComExecCommandParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * udp异频执行与监听
     */
    @Test
    public void testSimpleClientForUDP() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "udp", "u3", "u3", "d3", 0, true);
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);
                //直接传字符串
//                WsUdpExecCommandParam wsUdpExecCommandParam = new WsUdpExecCommandParam("xuedianfu");
                //传字节数组
                byte[] bytes = hc.toBytes("1CFE00000000FFE7");
                WsUdpExecCommandParam wsUdpExecCommandParam = new WsUdpExecCommandParam(bytes);
                distributedWsSimpleClient.send(wsUdpExecCommandParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * 数据推送监听
     */
    @Test
    public void testSimpleClientForPush() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "push", "u3", "u3", "d3");
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        WsPushSubscriptionParam wsPushSubscriptionParam = new WsPushSubscriptionParam();
        wsPushSubscriptionParam.add(0);
        wsPushSubscriptionParam.add(1);
        distributedWsSimpleClient.send(wsPushSubscriptionParam);

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }

    /**
     * BACNet异频执行与监听
     */
    @Test
    public void testSimpleClientForBacnet() {
        DistributedRegistyUris distributedRegistyUris = new DistributedRegistyUris();
        RegistyUri registyUri1 = new RegistyUri("http://127.0.0.1:10014", 0);
        distributedRegistyUris.add(registyUri1);
        DistributedWsSimpleClient distributedWsSimpleClient = DistributedWsSimpleClient.getInstance(distributedRegistyUris, "bacnet", "u3", "u3", "d3", 0);
        distributedWsSimpleClient.addListener(new SimpleClientMessageAdapter() {
            @Override
            public void call(String body) {
                System.out.println(body);
            }
        });

        WsBacnetSubscriptionParam wsBacnetSubscriptionParam = new WsBacnetSubscriptionParam();
        WsBacNetBaseItem wsBacNetBaseItem1 = new WsBacNetBaseItem();
        wsBacNetBaseItem1.setTag("tag1");
        wsBacNetBaseItem1.setDeviceId(3113167);
        wsBacNetBaseItem1.setProperty(85);
        wsBacNetBaseItem1.setDatatype(0);
        wsBacNetBaseItem1.setInstance(0);
        wsBacnetSubscriptionParam.add(wsBacNetBaseItem1);
        WsBacNetBaseItem wsBacNetBaseItem2 = new WsBacNetBaseItem();
        wsBacNetBaseItem2.setTag("tag2");
        wsBacNetBaseItem2.setDeviceId(3113168);
        wsBacNetBaseItem2.setProperty(85);
        wsBacNetBaseItem2.setDatatype(0);
        wsBacNetBaseItem2.setInstance(0);
        wsBacnetSubscriptionParam.add(wsBacNetBaseItem2);
        distributedWsSimpleClient.send(wsBacnetSubscriptionParam);

        try {
            while (true) {
                distributedWsSimpleClient.waiting(5);

                WsBacNetPublishParam wsBacNetPublishParam = new WsBacNetPublishParam();
                WsBacNetPublishItem item1 = new WsBacNetPublishItem();
                item1.setTag("tag1");
                item1.setDeviceId(3113167);
                item1.setProperty(85);
                item1.setDatatype(19);
                item1.setInstance(0);
                item1.setIsWrite(true);
                item1.setValue(88);
                wsBacNetPublishParam.add(item1);
                WsBacNetPublishItem item2 = new WsBacNetPublishItem();
                item2.setTag("tag2");
                item2.setDeviceId(3113168);
                item2.setProperty(85);
                item2.setDatatype(2);
                item2.setInstance(2);
                item2.setIsWrite(true);
                item2.setValue(99.99);
                wsBacNetPublishParam.add(item2);
                distributedWsSimpleClient.send(wsBacNetPublishParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedWsSimpleClient.close();
        }
    }
}
